(function () {
    $('#usrAccount').on('change', function () {
        var usrAccount = $('#usrAccount').val()
        var ajaxresult = $('#ajaxresult');
        var submit = $('#submit');
        $.ajax({
            url: '/checkAccount',
            method: 'post',
            data: { usrAccount: usrAccount },
            success: function (jsonResult) {
                console.log(jsonResult)
                if (jsonResult.status == 1) {
                    ajaxresult.text('√')
                    ajaxresult.css({color:'green'})
                    submit.prop('disabled', false)
                } else {
                    ajaxresult.text('×')
                    ajaxresult.css({color: 'red'})
                    submit.prop('disabled', true)
                }
            },
            error: function () {
                layer.msg('服务器异常！')
            }
        })
    })
    $('#submit').on('click', function () {
        var usrName = $('#usrName').val();
        var usrAccount = $('#usrAccount').val();
        var usrPassword = $('#usrPassword').val();
        var password = $('#password').val();
        var ADMIN = $('#admin').val();
        var USER = $('#user').val();
        var SYSTEM = $('#system').val();
        if (!/^[\u4e00-\u9fa5_a-zA-Z0-9_]{2,16}$/.test(usrName)){
            layer.tips('姓名不正确！', '#usrName', {tips:[1,'rgba(0,0,0,0.5)'], time:3000})
            return;
        }
        if (!/^\w{2,16}$/.test(usrAccount)){
            layer.tips('账号不正确！', '#usrAccount', {tips:[1,'rgba(0,0,0,0.5)'], time:3000})
            return;
        }
        if (!/^\w{2,16}$/.test(usrPassword)){
            layer.tips('密码不正确！', '#usrPassword', {tips:[1,'rgba(0,0,0,0.5)'], time:3000})
            return;
        }
        if (usrPassword !== password){
            layer.tips('两次输入的密码不一致！', '#password', {tips:[1,'rgba(0,0,0,0.5)'], time:3000})
            return;
        }
        if (!$('#admin').prop("checked") && !$('#user').prop("checked") && !$('#system').prop("checked")){
            layer.tips('请选择一个用户角色！', '#admin', {tips:[1,'rgba(0,0,0,0.5)'], time:3000})
            return;
        }
        $.ajax({
            url: '/register',
            method: 'post',
            data: {usrName: usrName, usrAccount: usrAccount, usrPassword: usrPassword, usrRole: $('#admin').prop("checked") ? ADMIN : $('#user').prop("checked") ? USER : $('#system').prop("checked") ? SYSTEM : null},
            success: function (jsonResult) {
                if (jsonResult.status == 1) location.href = '/'
                else layer.msg(jsonResult.message, {icon: 5})
            },
            error: function () {
                layer.msg('服务器异常！')
            }
        })
    })
})();